<?php
header("Content-Type:text/html; charset=utf-8");

if ($_FILES["file_upload"]["error"] > 0)
{
	echo "Error: " . $_FILES["file_upload"]["error"] . "<br />";
}
else
{
	echo "Upload: " . $_FILES["file_upload"]["name"] . "<br />";
	echo "Type: " . $_FILES["file_upload"]["type"] . "<br />";
	echo "Size: " . ($_FILES["file_upload"]["size"] / 1024) . " Kb<br />";
	echo "Stored in: " . $_FILES["file_upload"]["tmp_name"]."<br />";
	
	$upload_dir = $_SERVER['DOCUMENT_ROOT']."/upload";
	$tmp_dir = $_SERVER['DOCUMENT_ROOT']."/temp";
	
	$file = $_FILES["file_upload"]["name"];
	$tmpfile = $_FILES["file_upload"]["tmp_name"];
	
	echo "上传文件保存目录：".$upload_dir."<br />";
	echo "解压文件路径：".$tmp_dir."<br />";
	echo "保存文件：".$file."<br />";
	echo "临时文件：".$tmpfile."<br />";
	
	//exit("111");
	if(!file_exists($upload_dir)) {
		if(mkdir($upload_dir, 0, true)) {
			echo "created upload dir: ".$upload_dir."<br />";
		}
		else {
			exit("Failed to create upload dir");
		}
	}
	
	if(!file_exists($tmp_dir)) {
		if(mkdir($tmp_dir, 0, true)) {
			echo "created temp dir: ".$tmp_dir."<br />";
		}
		else {
			exit("Failed to create upload dir");
		}
	}
	
	$fileupload = $upload_dir.'/'.$file;
	$fileupload_gbk = iconv('UTF-8', 'GB2312', $fileupload);
	if (file_exists($fileupload_gbk))
	{
		echo $fileupload." 已经存在."."<br />";
		if(unlink($fileupload_gbk)) {
			echo $fileupload." 已被删除."."<br />";
		}
		else {
			echo "删除失败"."<br />";
		}
	}
	
	if(move_uploaded_file($tmpfile, $fileupload_gbk))
	{
		echo "成功保存文件到: " . $fileupload."<br />";
		echo "开始解压文件: ".$fileupload."<br />";
		$zip = new ZipArchive();
		$rs = $zip->open($fileupload_gbk);
			
		if($rs !== TRUE)
		{
			die('解压失败! Error Code:'. $rs);
		}
			
		$zip->extractTo($tmp_dir);
		$zip->close();
			
		echo '解压成功!'."<br />";
	}
	else{
		echo "保存文件出错."."<br />";
	}
}
?>